package com.lagou.edu.service.impl;

import com.lagou.edu.mapper.AccountMapper;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public List<Account> queryAccountList() throws Exception {
        return accountMapper.queryAccountList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
        try {
            Account from = accountMapper.queryAccountByCardNo(fromCardNo);
            Account to = accountMapper.queryAccountByCardNo(toCardNo);
            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);
            accountMapper.updateAccountByCardNo(from);
            int i = 1/0;
        } catch (Exception e) {
            throw new Exception("失败:" + e.getMessage());
        }
    }
}
